﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace YJ.Data.MSSQL
{
    public class Menu : YJ.Data.Interface.IMenu
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public Menu()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.Menu实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.Menu model)
        {
            string sql = @"INSERT INTO Menu
				(ID,ParentID,AppLibraryID,Title,Params,Ico,Sort,IcoColor) 
				VALUES(@ID,@ParentID,@AppLibraryID,@Title,@Params,@Ico,@Sort,@IcoColor)";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID },
				new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier, -1){ Value = model.ParentID },
				model.AppLibraryID == null ? new SqlParameter("@AppLibraryID", SqlDbType.UniqueIdentifier, -1) { Value = DBNull.Value } : new SqlParameter("@AppLibraryID", SqlDbType.UniqueIdentifier, -1) { Value = model.AppLibraryID },
				new SqlParameter("@Title", SqlDbType.NVarChar, 1000){ Value = model.Title },
				model.Params == null ? new SqlParameter("@Params", SqlDbType.VarChar, 5000) { Value = DBNull.Value } : new SqlParameter("@Params", SqlDbType.VarChar, 5000) { Value = model.Params },
				model.Ico == null ? new SqlParameter("@Ico", SqlDbType.VarChar, 500) { Value = DBNull.Value } : new SqlParameter("@Ico", SqlDbType.VarChar, 500) { Value = model.Ico },
				new SqlParameter("@Sort", SqlDbType.Int, -1){ Value = model.Sort },
                model.IcoColor == null ? new SqlParameter("@IcoColor", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@IcoColor", SqlDbType.VarChar, 50) { Value = model.IcoColor }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.Menu实体类</param>
        public int Update(YJ.Data.Model.Menu model)
        {
            string sql = @"UPDATE Menu SET 
				ParentID=@ParentID,AppLibraryID=@AppLibraryID,Title=@Title,Params=@Params,Ico=@Ico,Sort=@Sort,IcoColor=@IcoColor 
				WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier, -1){ Value = model.ParentID },
				model.AppLibraryID == null ? new SqlParameter("@AppLibraryID", SqlDbType.UniqueIdentifier, -1) { Value = DBNull.Value } : new SqlParameter("@AppLibraryID", SqlDbType.UniqueIdentifier, -1) { Value = model.AppLibraryID },
				new SqlParameter("@Title", SqlDbType.NVarChar, 1000){ Value = model.Title },
				model.Params == null ? new SqlParameter("@Params", SqlDbType.VarChar, 5000) { Value = DBNull.Value } : new SqlParameter("@Params", SqlDbType.VarChar, 5000) { Value = model.Params },
				model.Ico == null ? new SqlParameter("@Ico", SqlDbType.VarChar, 500) { Value = DBNull.Value } : new SqlParameter("@Ico", SqlDbType.VarChar, 500) { Value = model.Ico },
				new SqlParameter("@Sort", SqlDbType.Int, -1){ Value = model.Sort },
                model.IcoColor == null ? new SqlParameter("@IcoColor", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@IcoColor", SqlDbType.VarChar, 50) { Value = model.IcoColor },
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM Menu WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.Menu> DataReaderToList(SqlDataReader dataReader)
        {
            List<YJ.Data.Model.Menu> List = new List<YJ.Data.Model.Menu>();
            YJ.Data.Model.Menu model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.Menu();
                model.ID = dataReader.GetGuid(0);
                model.ParentID = dataReader.GetGuid(1);
                if (!dataReader.IsDBNull(2))
                    model.AppLibraryID = dataReader.GetGuid(2);
                model.Title = dataReader.GetString(3);
                if (!dataReader.IsDBNull(4))
                    model.Params = dataReader.GetString(4);
                if (!dataReader.IsDBNull(5))
                    model.Ico = dataReader.GetString(5);
                model.Sort = dataReader.GetInt32(6);
                if (!dataReader.IsDBNull(7))
                    model.IcoColor = dataReader.GetString(7);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.Menu> GetAll()
        {
            string sql = "SELECT * FROM Menu";
            SqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.Menu> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM Menu";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.Menu Get(Guid id)
        {
            string sql = "SELECT * FROM Menu WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.Menu> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public System.Data.DataTable GetAllDataTable()
        {
            string sql = "SELECT a.*,b.Address,b.OpenMode,b.Width,b.Height,b.Params AS Params1,b.Ico AS AppIco,b.Color AS IcoColor1 FROM Menu a LEFT JOIN AppLibrary b ON a.AppLibraryID=b.ID ORDER BY a.Sort";          
            return dbHelper.GetDataTable(sql);
        }

        /// <summary>
        /// 查询下级记录
        /// </summary>
        public List<YJ.Data.Model.Menu> GetChild(Guid id)
        {
            string sql = "SELECT * FROM Menu WHERE ParentID=@ParentID ORDER BY Sort";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.Menu> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 是否有下级记录
        /// </summary>
        public bool HasChild(Guid id)
        {
            string sql = "SELECT TOP 1 ID FROM Menu WHERE ParentID=@ParentID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            bool has = dataReader.HasRows;
            dataReader.Close();
            return has;
        }

        /// <summary>
        /// 更新排序
        /// </summary>
        public int UpdateSort(Guid id, int sort)
        {
            string sql = "UPDATE Menu SET Sort=@Sort WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@Sort", SqlDbType.Int){ Value = sort },
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }

        /// <summary>
        /// 得到最大排序值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetMaxSort(Guid id)
        {
            string sql = "SELECT ISNULL(MAX(Sort),0)+1 FROM Menu WHERE ParentID=@ParentID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            string max = dbHelper.GetFieldValue(sql, parameters);
            int max1;
            return max.IsInt(out max1) ? max1 : 1;
        }


        /// <summary>
        /// 查询一个应用程序库所有记录
        /// </summary>
        public List<YJ.Data.Model.Menu> GetAllByApplibaryID(Guid applibaryID)
        {
            string sql = "SELECT * FROM Menu WHERE AppLibraryID=@AppLibraryID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@AppLibraryID", SqlDbType.UniqueIdentifier){ Value = applibaryID }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.Menu> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}